package org.millioncall.yueyoga.admin.action.pad;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FilenameUtils;
import org.millioncall.openplatform.common.Pager;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.util.IDGeneratorUtils;
import org.millioncall.openplatform.util.StringUtils;
import org.millioncall.openplatform.util.WebAppUtils;
import org.millioncall.yueyoga.admin.model.ClassScheduleModel;
import org.millioncall.yueyoga.admin.model.ConsumeHistoryModel;
import org.millioncall.yueyoga.admin.model.Group;
import org.millioncall.yueyoga.admin.model.Hall;
import org.millioncall.yueyoga.admin.model.LoginUser;
import org.millioncall.yueyoga.admin.model.MemberModel;
import org.millioncall.yueyoga.admin.model.Product;
import org.millioncall.yueyoga.admin.model.Reservation;
import org.millioncall.yueyoga.admin.model.TinyClass;
import org.millioncall.yueyoga.admin.model.User;
import org.millioncall.yueyoga.admin.service.CheduleService;
import org.millioncall.yueyoga.admin.service.ClazzService;
import org.millioncall.yueyoga.admin.service.HallService;
import org.millioncall.yueyoga.admin.service.MemberService;
import org.millioncall.yueyoga.admin.service.ProductService;
import org.millioncall.yueyoga.admin.service.ReservationService;
import org.millioncall.yueyoga.admin.service.UserService;
import org.millioncall.yueyoga.admin.service.impl.UserServiceImpl.LoginResult;
import org.millioncall.yueyoga.common.utils.AjaxUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializerFeature;

@Controller
public class MainAction extends BasePadAction {
	private static final Logger LOGGER = LoggerFactory
			.getLogger(MainAction.class);
	@Resource(name = "userService")
	private UserService userService;
	@Resource(name = "hallService")
	private HallService hallService;
	@Resource(name = "cheduleService")
	private CheduleService cheduleService;
	@Resource(name = "clazzService")
	private ClazzService clazzService;
	@Resource(name = "memberService")
	private MemberService memberService;
	@Resource(name = "productService")
	private ProductService productService;
	@Resource(name = "reservationService")
	private ReservationService reservationService;

	// 跳转到登录页
	@RequestMapping(value = "/toLogin.jhtml")
	public String toLogin(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		super.initFrontHeader(request, model);
		List<Hall> halls = hallService.findHalls();
		model.put("halls", halls);
		String loginError = (String) request.getSession().getAttribute(
				"loginErrorMsg");
		if (StringUtils.isNotBlank(loginError)) {
			model.put("loginErrorMsg", loginError);
			request.getSession().removeAttribute("loginErrorMsg");
		}
		return "login";
	}

	// 用户注销
	@RequestMapping(value = "/ajaxLogout.jhtml")
	public String toLogout(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		request.getSession().removeAttribute("padLoginUser");
		return "redirect:toLogin.jhtml";
	}

	// 登录提交
	@RequestMapping(value = "/submitLogin.jhtml")
	public String submitLogin(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {

		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String hall = request.getParameter("hall");

		LoginUser lu = new LoginUser();
		lu.setUsername(username);
		lu.setPassword(password);
		lu.setOrganization(hall);
		lu.setLoginTime(new Date());// 设置登录时间
		lu.setLoginIp(WebAppUtils.getClientIp(request));
		// 使用UserService类对登录用户对象进行登录认证
		LoginResult lr = userService.userLogin(lu, null);

		if (!lr.isSuccess()) {
			request.getSession().setAttribute("loginErrorMsg",
					lr.getResultMsg());
			model.clear();
			return "redirect:toLogin.jhtml";
		}
		request.getSession().setAttribute("padLoginUser", lr.getUser());
		model.clear();
		return "redirect:main.jhtml";
	}

	// 跳转到pad主页
	@RequestMapping(value = "/index.jhtml")
	public String toIndex(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		return "forward:main.jhtml";
	}

	// 主页
	@RequestMapping(value = "/main.jhtml")
	public String toMain(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		User u = super.getPadLoginUser(request);
		if (u == null) {
			return "redirect:toLogin.jhtml";
		}

		super.initFrontHeader(request, model);
		Map<String, Object> map = new HashMap<String, Object>();
		User user = super.getPadLoginUser(request);

		map.put("hallcode", user.getOrganizationId());
		List<TinyClass> clazzList = this.clazzService.findAllClassTiny(map);
		model.put("clazzList", clazzList);

		// 产品名称
		List<Product> products = productService.findProductAvailable(1,
				Integer.MAX_VALUE, map);
		model.put("products", products);

		return "main";
	}

	// 查询课程列表
	@RequestMapping(value = "/ajaxGetLessionsByDate.jhtml")
	public void ajaxGetLessionsByDate(HttpServletRequest request,
			HttpServletResponse response) {
		String json = "{'success':false}";
		User user = getPadLoginUser(request);

		String clazzId = request.getParameter("clazzId");
		String kcrq = request.getParameter("kcrq");
		String memberId = request.getParameter("memberId");

		if (StringUtils.isNotBlank(kcrq)) {
			List<ClassScheduleModel> csmList = cheduleService
					.findScheduleInfoList(clazzId, kcrq,
							user.getOrganizationId(), memberId);
			json = "{\"total\":" + csmList.size() + ",\"rows\":"
					+ JSON.toJSONString(csmList) + "}";
		}
		AjaxUtils.ajaxResponse(response, json);
	}

	// 查询课程日期列表
	@RequestMapping(value = "/ajaxGetLessionsDateList.jhtml")
	public void ajaxGetLessionDateList(HttpServletRequest request,
			HttpServletResponse response) {
		Calendar can = Calendar.getInstance();
		int week = can.get(Calendar.DAY_OF_WEEK) - 1;// 周几
		if (week == 0) {
			week = 7;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");

		// 今天的日期字符串
		String today = new SimpleDateFormat("yyyy-MM-dd").format(can.getTime());

		can.add(Calendar.DAY_OF_WEEK, 1 - week);
		Date mondy = can.getTime();
		List<String> week1 = new ArrayList<String>();
		week1.add(sdf.format(mondy));

		for (int i = 0; i < 6; i++) {
			can.add(Calendar.DAY_OF_WEEK, 1);
			Date weekday = can.getTime();
			week1.add(sdf.format(weekday));
		}

		List<String> week2 = new ArrayList<String>();
		for (int i = 0; i < 7; i++) {
			can.add(Calendar.DAY_OF_WEEK, 1);
			Date weekday = can.getTime();
			week2.add(sdf.format(weekday));
		}

		String weekstr1 = JSON.toJSONString(week1);
		String weekstr2 = JSON.toJSONString(week2);

		AjaxUtils.ajaxResponse(response, "{\"success\":true,\"today\":\""
				+ today + "\",\"week\":" + week + ",\"week1\":" + weekstr1
				+ ",\"week2\":" + weekstr2 + "}");
	}

	// 查询会员列表
	@RequestMapping(value = "/ajaxGetMemberList.jhtml")
	public void ajaxGetMemberList(HttpServletRequest request,
			HttpServletResponse response) {
		String json = "{\"success\":false}";
		String page = request.getParameter("page");
		String rows = request.getParameter("rows");

		int start = 0;
		int limit = 10;

		if (StringUtils.isNotBlank(rows)) {
			limit = Integer.valueOf(rows);
		}

		if (StringUtils.isNotBlank(page)) {
			start = (Integer.valueOf(page).intValue() - 1) * limit;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		User user = super.getPadLoginUser(request);
		map.put("hallcode", user.getOrganizationId());
		map.put("cardNumber", "");

		// 根据条件查询
		String hykh = request.getParameter("hykh");// 会员卡号
		String hysjh = request.getParameter("hysjh");// 会员手机号
		String hyxm = request.getParameter("hyxm");// 会员姓名
		String hykx = request.getParameter("hykx");// 会员卡项

		if (StringUtils.isNotBlank(hykh)) {
			map.put("hykh", "%" + hykh + "%");
		}

		if (StringUtils.isNotBlank(hysjh)) {
			map.put("hysjh", "%" + hysjh + "%");
		}

		if (StringUtils.isNotBlank(hyxm)) {
			map.put("hyxm", "%" + hyxm + "%");
		}

		if (StringUtils.isNotBlank(hykx)) {
			map.put("hykx", hykx);
		}

		PaginatedList<MemberModel> memberList = memberService
				.findFrontMemberList(start, limit, map);
		Pager pager = memberList.getPager();
		String memberItems = JSON.toJSONStringWithDateFormat(memberList,
				"yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat);

		json = "{\"total\":" + pager.getCountOfTotalResults().intValue()
				+ ",\"rows\":" + memberItems + "}";
		AjaxUtils.ajaxResponse(response, json);
	}

	// 查询在店会员
	@RequestMapping(value = "/ajaxGetPresentMemberList.jhtml")
	public void ajaxGetPresentMemberList(HttpServletRequest request,
			HttpServletResponse response) {

		Map<String, Object> map = new HashMap<String, Object>();
		User user = super.getPadLoginUser(request);
		map.put("hallcode", user.getOrganizationId());
		map.put("findType", "present");

		Date today = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		try {
			map.put("timeRangeStart", sdf.parse(sdf.format(today)));// 今天
		} catch (ParseException e) {
			LOGGER.error("日期解析异常", e);
		}

		Calendar can = Calendar.getInstance();
		can.setTime(today);
		can.add(Calendar.DAY_OF_MONTH, 1);

		try {
			map.put("timeRangeEnd", sdf.parse(sdf.format(can.getTime())));// 明天
		} catch (ParseException e) {
			LOGGER.error("日期解析异常", e);
		}

		sdf = new SimpleDateFormat("HH:mm");
		map.put("timeLimit", sdf.format(today));

		String page = request.getParameter("page");
		String rows = request.getParameter("rows");

		int start = 0;
		int limit = 10;

		if (StringUtils.isNotBlank(rows)) {
			limit = Integer.valueOf(rows);
		}

		if (StringUtils.isNotBlank(page)) {
			start = (Integer.valueOf(page).intValue() - 1) * limit;
		}

		PaginatedList<MemberModel> memberList = memberService
				.findFrontPresentMemberList(start, limit, map);
		Pager pager = memberList.getPager();

		String memberItems = JSON.toJSONStringWithDateFormat(memberList,
				"yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat);

		String json = "{\"success\":true,\"total\":"
				+ pager.getCountOfTotalResults().intValue() + ",rows:"
				+ memberItems + "}";
		AjaxUtils.ajaxResponse(response, json);
	}

	// 查询离店会员
	@RequestMapping(value = "/ajaxGetUnPresentMemberList.jhtml")
	public void ajaxGetUnPresentMemberList(HttpServletRequest request,
			HttpServletResponse response) {

		Map<String, Object> map = new HashMap<String, Object>();
		User user = super.getPadLoginUser(request);
		map.put("hallcode", user.getOrganizationId());
		map.put("findType", "absent");

		Date today = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		try {
			map.put("timeRangeStart", sdf.parse(sdf.format(today)));// 今天
		} catch (ParseException e) {
			LOGGER.error("日期解析异常", e);
		}

		Calendar can = Calendar.getInstance();
		can.setTime(today);
		can.add(Calendar.DAY_OF_MONTH, 1);

		try {
			map.put("timeRangeEnd", sdf.parse(sdf.format(can.getTime())));// 明天
		} catch (ParseException e) {
			LOGGER.error("日期解析异常", e);
		}

		sdf = new SimpleDateFormat("HH:mm");
		map.put("timeLimit", sdf.format(today));

		String page = request.getParameter("page");
		String rows = request.getParameter("rows");

		int start = 0;
		int limit = 10;

		if (StringUtils.isNotBlank(rows)) {
			limit = Integer.valueOf(rows);
		}

		if (StringUtils.isNotBlank(page)) {
			start = (Integer.valueOf(page).intValue() - 1) * limit;
		}

		PaginatedList<MemberModel> memberList = memberService
				.findFrontPresentMemberList(start, limit, map);
		Pager pager = memberList.getPager();

		String memberItems = JSON.toJSONStringWithDateFormat(memberList,
				"yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat);

		String json = "{\"success\":true,\"total\":"
				+ pager.getCountOfTotalResults().intValue() + ",rows:"
				+ memberItems + "}";
		AjaxUtils.ajaxResponse(response, json);
	}

	// 查询考勤记录
	@RequestMapping(value = "/ajaxGetConsumeList.jhtml")
	public void ajaxGetConsumeList(HttpServletRequest request,
			HttpServletResponse response) {

		String classId = request.getParameter("classId");
		String productId = request.getParameter("productId");
		String memberId = request.getParameter("memberId");
		String areaId = request.getParameter("areaId");
		String consumeTimeFrom = request.getParameter("consumeTimeFrom");
		String consumeTimeTo = request.getParameter("consumeTimeTo");
		String scheduleTimeFrom = request.getParameter("scheduleTimeFrom");
		String scheduleTimeTo = request.getParameter("scheduleTimeTo");

		Map<String, Object> params = new HashMap<String, Object>();
		if (StringUtils.isNotBlank(classId)) {
			params.put("classId", classId);
		}

		if (StringUtils.isNotBlank(productId)) {
			params.put("productId", productId);
		}

		if (StringUtils.isNotBlank(memberId)) {
			params.put("memberId", memberId);
		}

		if (StringUtils.isNotBlank(areaId)) {
			params.put("areaId", areaId);
		}

		if (StringUtils.isNotBlank(consumeTimeFrom)) {
			params.put("consumeTimeFrom", consumeTimeFrom);
		}

		if (StringUtils.isNotBlank(consumeTimeTo)) {
			params.put("consumeTimeTo", consumeTimeTo);
		}

		if (StringUtils.isNotBlank(scheduleTimeFrom)) {
			params.put("scheduleTimeFrom", scheduleTimeFrom);
		}
		if (StringUtils.isNotBlank(scheduleTimeTo)) {
			params.put("scheduleTimeTo", scheduleTimeTo);
		}

		String page = request.getParameter("page");
		String rows = request.getParameter("rows");

		int pageNo = 1;
		int limit = 10;

		if (StringUtils.isNotBlank(rows)) {
			limit = Integer.valueOf(rows);
		}

		if (StringUtils.isNotBlank(page)) {
			pageNo = Integer.valueOf(page);
		}

		if (pageNo < 1) {
			pageNo = 1;
		}

		List<Integer> groupIds = new ArrayList<Integer>();
		for (Group group : super.getPadLoginUser(request).getGroups()) {
			groupIds.add(group.getId());
		}
		params.put("groups", groupIds);

		PaginatedList<ConsumeHistoryModel> consumeList = this.productService
				.findAllConsumeHistory(params, pageNo, limit);
		Pager pager = consumeList.getPager();

		String consumeItems = JSON.toJSONString(consumeList);

		String json = "{\"success\":true,\"total\": "
				+ pager.getCountOfTotalResults().intValue() + ",\"rows\":"
				+ consumeItems + "}";
		AjaxUtils.ajaxResponse(response, json);
	}

	// 查询会员详细信息
	@RequestMapping(value = "/ajaxSearchAndShowMember.jhtml")
	public void ajaxSearchAndShowMember(HttpServletRequest request,
			HttpServletResponse response) {
		String json = "{\"success\":false}";
		String page = request.getParameter("page");
		String rows = request.getParameter("rows");

		int start = 0;
		int limit = 10;

		if (StringUtils.isNotBlank(rows)) {
			limit = Integer.valueOf(rows);
		}

		if (StringUtils.isNotBlank(page)) {
			start = (Integer.valueOf(page).intValue() - 1) * limit;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		User user = super.getPadLoginUser(request);
		map.put("hallcode", user.getOrganizationId());
		map.put("cardNumber", "");

		// 根据条件查询
		String content = request.getParameter("content");// 会员卡号或姓名或卡外码

		if (StringUtils.isNotBlank(content)) {
			map.put("content", "%" + content + "%");
		}

		PaginatedList<MemberModel> memberList = memberService
				.findFrontMemberList(start, limit, map);
		Pager pager = memberList.getPager();
		String memberItems = JSON.toJSONStringWithDateFormat(memberList,
				"yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat);

		json = "{\"success\":true,\"total\":"
				+ pager.getCountOfTotalResults().intValue() + ",\"rows\":"
				+ memberItems + "}";
		AjaxUtils.ajaxResponse(response, json);
	}

	// 查询会员详细信息
	@RequestMapping(value = "/ajaxGetMemberDetail.jhtml")
	public void ajaxGetMemberDetail(HttpServletRequest request,
			HttpServletResponse response) {
		String json = "{'success':false}";
		Map<String, Object> map = new HashMap<String, Object>();
		String memberId = request.getParameter("memberId");

		if (StringUtils.isNotBlank(memberId)) {
			User user = super.getPadLoginUser(request);
			map.put("hallcode", user.getOrganizationId());
			map.put("id", memberId);

			MemberModel member = memberService.findFrontMemberDetailInfo(map);

			json = "{\"success\":true,\"data\":"
					+ JSON.toJSONStringWithDateFormat(member, "yyyy-MM-dd",
							SerializerFeature.WriteDateUseDateFormat) + "}";
		}
		AjaxUtils.ajaxResponse(response, json);
	}

	// 上传个人头像
	@RequestMapping(value = "ajaxUploadHeadImg.jhtml")
	public void ajaxUploadHeadImg(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "file", required = false) MultipartFile file) {
		if (file == null) {
			AjaxUtils.ajaxResponse(response,
					"{\"success\":false,\"error\":\"未上传头像图片\"}");
			return;
		}

		String origName = file.getOriginalFilename();// 原始文件名，即上传的文件名
		if (StringUtils.isBlank(origName)) {
			AjaxUtils.ajaxResponse(response,
					"{\"success\":false,\"error\":\"原始头像图片名称为空\"}");
			return;
		}

		String ext = FilenameUtils.getExtension(origName).toLowerCase();// 文件扩展名
		String uploaddir = "/upload/";
		String realpath = request.getServletContext().getRealPath(uploaddir);
		if (!realpath.endsWith(File.separator)) {
			realpath += File.separator;
		}

		File filedir = new File(realpath);
		if (!filedir.exists()) {
			filedir.mkdirs();
		}

		String uuid = IDGeneratorUtils.UUID32Id();
		String filename = uuid + "." + ext;
		String fileUrl = "";

		File tempFile = new File(realpath + filename);
		try {
			file.transferTo(tempFile);
		} catch (Exception e) {
			LOGGER.error("文件上传异常", e);
		}
		fileUrl = uploaddir + tempFile.getName();

		String memberId = request.getParameter("memberId");
		if (!StringUtils.isBlank(memberId)) {
			Map<String, String> params = new HashMap<String, String>();
			String path = request.getServletContext().getRealPath("/");
			params.put("memberId", memberId);
			params.put("memberHeadImg", fileUrl);
			params.put("path", path);
			this.memberService.updateMemberHeadImg(params);
		}
		AjaxUtils.ajaxResponse(response, "{\"success\":true,\"path\":\""
				+ fileUrl + "\"}");
	}

	// 查询今天或明天的课程
	@RequestMapping(value = "ajaxGetDayLessionList.jhtml")
	public void ajaxGetDayLessionList(HttpServletRequest request,
			HttpServletResponse response) {

		String kcrq = request.getParameter("kcrq");
		String memberId = request.getParameter("memberId");
		String clazzId = request.getParameter("clazzId");

		String json = "{\"success\":false}";
		User user = super.getPadLoginUser(request);

		if (StringUtils.isNotBlank(kcrq)) {
			List<ClassScheduleModel> csmList = cheduleService
					.findScheduleInfoList(clazzId, kcrq,
							user.getOrganizationId(), memberId);
			json = "{\"success\":true, \"rows\":" + JSON.toJSONString(csmList)
					+ "}";
		}
		AjaxUtils.ajaxResponse(response, json);
	}

	// 查询个人已预定的课程列表
	@RequestMapping(value = "/ajaxReserveLessionList.jhtml")
	public void ajaxReserveLessionList(HttpServletRequest request,
			HttpServletResponse response) {
		String json = "{\"success\":false,\"msg\":\"查询课程列表时异常\"}";

		String searchDate = request.getParameter("searchDate");
		String memberId = request.getParameter("memberId");
		String clazzId = request.getParameter("clazzId");

		if (StringUtils.isBlank(searchDate) || StringUtils.isBlank(memberId)) {
			AjaxUtils.ajaxResponse(response, json);
			return;
		}
		User user = super.getPadLoginUser(request);
		List<ClassScheduleModel> csmList = cheduleService
				.findPersonScheduleList(clazzId, searchDate, memberId,
						user.getOrganizationId());

		json = "{\"success\":true, \"rows\":" + JSON.toJSONString(csmList)
				+ "}";
		AjaxUtils.ajaxResponse(response, json);
	}

	// 预约课程
	@RequestMapping(value = "/ajaxMemberReserveLession.jhtml")
	public void ajaxMemberReserveLession(HttpServletRequest request,
			HttpServletResponse response) {
		String json = "{\"success\":false}";

		String scheId = request.getParameter("scheId");
		String memberId = request.getParameter("memberId");

		if (StringUtils.isBlank(scheId) || StringUtils.isBlank(memberId)) {
			AjaxUtils.ajaxResponse(response, json);
			return;
		}
		int rtv = reservationService.resvSchedule(scheId, memberId);
		if (rtv == 1) {
			json = "{\"success\":true}";
		} else if (rtv > 1) {
			json = "{\"success\":false,\"error\":\"已预定此课程\"}";
		} else {
			json = "{\"success\":false,\"error\":\"预备课程失败\"}";
		}
		AjaxUtils.ajaxResponse(response, json);
	}

	// 取消预约课程
	@RequestMapping(value = "/ajaxMemberCancelReserveLession.jhtml")
	public void ajaxMemberCancelReserveLession(HttpServletRequest request,
			HttpServletResponse response) {
		String json = "{\"success\":false}";

		String scheduleId = request.getParameter("scheduleId");
		String memberId = request.getParameter("memberId");

		if (StringUtils.isBlank(scheduleId) || StringUtils.isBlank(memberId)) {
			AjaxUtils.ajaxResponse(response, json);
			return;
		}
		boolean flag = reservationService.cancelResvSchedule(scheduleId,
				memberId);
		if (flag) {
			json = "{\"success\":true}";
		} else {
			json = "{\"success\":false, \"error\":\"取消预定失败\"}";
		}
		AjaxUtils.ajaxResponse(response, json);
	}

	// 查询今日出席课程
	@RequestMapping(value = "/ajaxSearchAttendList.jhtml")
	public void ajaxSearchAttendList(HttpServletRequest request,
			HttpServletResponse response) {

		String json = "{\"success\":false}";
		String searchDate = request.getParameter("searchDate");
		String memberId = request.getParameter("memberId");
		String clazzId = request.getParameter("clazzId");

		if (StringUtils.isBlank(searchDate) || StringUtils.isBlank(memberId)) {
			AjaxUtils.ajaxResponse(response, json);
			return;
		}

		User user = super.getPadLoginUser(request);
		List<ClassScheduleModel> csmList = cheduleService
				.findAttendScheduleList(clazzId, searchDate, memberId,
						user.getOrganizationId());

		json = "{\"success\":true,\"rows\":" + JSON.toJSONString(csmList) + "}";
		AjaxUtils.ajaxResponse(response, json);
	}

	// 会员出席
	@RequestMapping(value = "/ajaxMemberAttend.jhtml")
	public void ajaxMemberAttend(HttpServletRequest request,
			HttpServletResponse response) {

		String json = "{\"success\":false, \"msg\":\"出席操作失败\"}";
		User user = super.getPadLoginUser(request);

		String resvId = request.getParameter("resvId");
		String productRemainingId = request.getParameter("prId");
		Integer prId = Integer.valueOf(productRemainingId);

		if (StringUtils.isBlank(resvId) || prId == null || prId == 0) {
			AjaxUtils.ajaxResponse(response, json);
			return;
		}

		json = reservationService.attendSchedule(resvId, prId, user);

		AjaxUtils.ajaxResponse(response, json);
	}

	// 会员取消出席
	@RequestMapping(value = "/ajaxCancelMemberAttend.jhtml")
	public void ajaxCancelMemberAttend(HttpServletRequest request,
			HttpServletResponse response) {

		String json = "{\"success\":false, \"msg\":\"取消出席操作失败\"}";

		String resvId = request.getParameter("resvId");
		String productRemainingId = request.getParameter("prId");
		Integer prId = Integer.valueOf(productRemainingId);

		if (StringUtils.isBlank(resvId) || prId == null || prId == 0) {
			WebAppUtils.ajaxResponse(response, json);
			return;
		}

		if (reservationService.cancelAttendSchedule(resvId, prId)) {
			json = "{\"success\":true, \"msg\":\"取消出席成功\"}";
		}
		AjaxUtils.ajaxResponse(response, json);
	}

	// 查询座位预定情况
	@RequestMapping(value = "/ajaxGetSeatReserveInfo.jhtml")
	public void ajaxGetSeatReserveInfo(HttpServletRequest request,
			HttpServletResponse response) {

		String kcxh = request.getParameter("kcxh");
		String searchDate = request.getParameter("searchDate");

		if (StringUtils.isBlank(kcxh) || StringUtils.isBlank(searchDate)) {
			AjaxUtils.ajaxResponse(response,
					"{\"success\":false,\"error\":\"查询条件不完整\"}");
			return;
		} else {
			Map<String, Object> maps = new HashMap<String, Object>();
			maps.put("cheduleTime", searchDate);
			maps.put("ydId", kcxh);
			List<Reservation> reList = reservationService
					.findReservationByCdts(maps);
			String items = JSONArray.toJSONString(reList);
			AjaxUtils.ajaxResponse(response, "{\"success\":true,\"rows\":"
					+ items + "}");
		}
	}
}
